package com.gm.wholesale.bluetooth.printBlue;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import androidx.core.app.ActivityCompat;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.bean.DeviceBean;
import com.gm.wholesale.bluetooth.interfaces.OnConnectionBluListener;
import com.gm.wholesale.bluetooth.service.BluetoothService;
import com.gm.wholesale.constant.Constant;
import com.youth.banner.util.LogUtils;

public class BasePrintBluethManger {


    private Handler mHandler;
    private OnConnectionBluListener mListener;

    public BluetoothService mBluetoothService;

    public BluetoothService getBluetoothService() {
        return mBluetoothService;
    }

    public String getConnectionDevice() {
        if (mBluetoothService != null) {
            return BluetoothUtil.newInstance().getConnectionDevice(mBluetoothService);
        }
        return null;
    }

    public DeviceBean getConnectionDevice(Context context) {
        if (mBluetoothService != null) {
            return BluetoothUtil.newInstance().getConnectionDevice(context, mBluetoothService);
        }
        return null;
    }

    public void setOnConnectionListener(OnConnectionBluListener listener) {
        mListener = listener;
    }

    public String getFilterString() {
        return Constant.ORDER_PRINT_BLU;
    }


    public void startConnection(Activity activity, String saveDevice, OnConnectionBluListener listener) {
        LoadingDialog.newLoadingDialog().show(activity);
        mListener = listener;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                showNotPermission();
                return;
            }
        }

        String devcieAddress = saveDevice;
        if (!TextUtils.isEmpty(devcieAddress)) {
            connection(activity, devcieAddress);
        } else {
            LoadingDialog.newLoadingDialog().hid();
            BluetoothUtil.newInstance().showBluDevices(activity, getFilterString(), device -> {
                if (device != null) {
                    connection(activity, device.getAddress());
                }
            });
        }

    }

    private void connection(Context context, String devcieAddress) {
        BluetoothUtil.newInstance().connectDevice(context, devcieAddress, getBluetoothService(context));
    }

    private BluetoothService getBluetoothService(Context context) {
        if (mHandler == null) {
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case 1:
                            break;
                        case 2:
                            if (msg.arg1 < 2) {
                                LogUtils.d("连接失败");
                                if (mListener != null) {
                                    mListener.connectionFail();
                                }
                            } else {
                                LogUtils.d("连接成功");
                                if (mListener != null) {
                                    if (mBluetoothService.mdevice != null) {
                                        DeviceBean deviceBean = new DeviceBean();
                                        deviceBean.setAddress(mBluetoothService.mdevice.getAddress());
                                        BluetoothUtil.setDevice(context,mBluetoothService.mdevice,deviceBean);
                                        mListener.connection(deviceBean);
                                    } else {
                                        mListener.connection(null);
                                    }

                                }

                            }
                    }
                }
            };
        }
        if (mBluetoothService == null) {
            mBluetoothService = new BluetoothService(context, mHandler);
        }
        return mBluetoothService;
    }

    // todo 通过地址直接连接
    public void connectByDevice(Context context, String address) {
        connection(context, address);
    }

    private void showNotPermission() {
        ToastUtil.showToast("没有蓝牙权限，请打开");
    }

    public int getConnectionState() {
        if (mBluetoothService != null)
            return mBluetoothService.getState();
        return 0;
    }

    public void print(String data) {
        if (mBluetoothService == null) return;
        BluetoothUtil.newInstance().print(data, mBluetoothService);
    }

    public void stop() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (mBluetoothService != null) {
            mBluetoothService.stop();
            mBluetoothService = null;
        }

    }


}
